package com.springboot.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.injector.ISqlInjector;
;
import com.springboot.domain.Role;
import com.springboot.service.RoleServiceI;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/rc")
public class RoleController {

    @Resource(name = "roleServiceI")
    private RoleServiceI rsi;

    @RequestMapping("/findRole")
    public List<Role> findRole(Role role){
        //条件构造器
        QueryWrapper qw = new QueryWrapper();
        if(role.getRoleName()!=null)
            qw.likeRight("role_name",role.getRoleName());//模糊查询 likeRight 内容% likeLeft %内容  like  %内容%
        if(role.getStatus()!=null)
            qw.eq("status",role.getStatus());//eq 等于  ne 不等于
        if(role.getStartTime()!=null)
            qw.ge("create_time",role.getStartTime());//gt 大于 ge大于等于 lt小于  le小于等于

        List<Role> list = rsi.list(qw);
        return list;
    }

    @RequestMapping("/addRole")
    public Map<String,Object> addRole(Role role){
        boolean b = rsi.save(role);
        Map<String,Object> map = new HashMap<>();
        map.put("msg","新增"+(b?"成功":"失败"));
        map.put("is",b);
        return map;
    }

    @RequestMapping("/editRole")
    public Map<String,Object> editRole(Role role){
        boolean b = rsi.saveOrUpdate(role);
        Map<String,Object> map = new HashMap<>();
        map.put("msg","更新"+(b?"成功":"失败"));
        map.put("is",b);
        return map;
    }

    @RequestMapping("/removeRole/{id}")
    public Map<String,Object> removeRole(@PathVariable(value = "id") int id){
        boolean b = rsi.removeById(id);
        Map<String,Object> map = new HashMap<>();
        map.put("msg","删除"+(b?"成功":"失败"));
        map.put("is",b);
        return map;
    }


    @RequestMapping("/findRole2")
    public List<Role> findRole2(){
        return rsi.findRole2();
    }

    @RequestMapping("/getMenuByRole2")
    public List<Role> getMenuByRole2(){
        return rsi.getMenuByRole2();
    }

}
